package controller;

import pojo.Student;
import service.StudentService;
import service.impl.StudentServiceimpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet("/stu")
public class studentServlet extends HttpServlet {
    List<Student> list = new ArrayList<Student>();
    StudentService studentService = new StudentServiceimpl();
    public void init() throws ServletException {
        this.getList();
    }

    public void getList(){
        studentService.getAllStu();

    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doHandle(req, resp);
    }


    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doHandle(req, resp);
    }

    private void doHandle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String action = req.getParameter("action");
        if ("list".equals(action)) {
            this.getList();
            req.setAttribute("students", list);
            RequestDispatcher dispatcher = req.getRequestDispatcher("student.jsp");
            dispatcher.forward(req, resp);
        }

         else if (action.equals("add")) {
            String name = req.getParameter("name");
            String stuNo = req.getParameter("stuNo");
            String gender = req.getParameter("gender");
            String grade = req.getParameter("grade");
            String depart = req.getParameter("depart");
            String major = req.getParameter("major");
            String stuClass = req.getParameter("class");

            Student student = new Student();
            student.setName(name);
            student.setStuNo(stuNo);
            student.setStuGender(gender);
            student.setStuGrade(grade);
            student.setStuDepart(depart);
            student.setStuMajor(major);
            student.setStuClass(stuClass);

            studentService.addStudent(student);

            RequestDispatcher dispatcher = req.getRequestDispatcher("stu?action=list");
            dispatcher.forward(req, resp);

//            try {
//                //1.加载驱动
//                Class.forName("com.mysql.cj.jdbc.Driver");
//
//                //2.创建数据库的链接  jdbc:mysql://hostname:port/databasename;
//                String url = "jdbc:mysql://localhost:3306/my?serverTimezone=GMT%2B8";
//                String user = "root";
//                String pwd = "1234";
//                Connection conn = DriverManager.getConnection(url, user, pwd);
//
//                //3.构造命令并执行命令
//                String sql = "insert into students(stuNo,name,stuGender,stuGrade,stuDepart,stuMajor,stuClass)values(?,?,?,?,?,?,?)";
//                PreparedStatement ps = conn.prepareStatement(sql);
//                ps.setString(1,stuNo);
//                ps.setString(2,name);
//                ps.setString(3,gender);
//                ps.setString(4,grade);
//                ps.setString(5,depart);
//                ps.setString(6,major);
//                ps.setString(7,stuClass);
//                ps.executeUpdate();
//                conn.close();
//                ps.close();
//
//                //返回界面
//                RequestDispatcher dispatcher = req.getRequestDispatcher("stu?action=list");
//                dispatcher.forward(req, resp);
//
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }

        }  else if (action.equals("delete")) {
            //获取要删除的学号
            String stuNo = req.getParameter("stuNo");
            studentService.deleteStudent(stuNo);
            RequestDispatcher dispatcher = req.getRequestDispatcher("stu?action=list");
            dispatcher.forward(req, resp);
            //过滤不显示当前要删除的行
//            list = list.stream().filter(u -> !u.getStuNo().equals(stuNo)).collect(Collectors.toList());
//            try {
//                //1.加载驱动
//                Class.forName("com.mysql.cj.jdbc.Driver");
//
//                //2.创建数据库的链接  jdbc:mysql://hostname:port/databasename;
//                String url = "jdbc:mysql://localhost:3306/my?serverTimezone=GMT%2B8";
//                String user = "root";
//                String pwd = "1234";
//                Connection conn = DriverManager.getConnection(url, user, pwd);
//
//                //3.构造命令并执行命令
//                String sql = "DELETE FROM students WHERE stuNo = '" +stuNo+"'";
//                Statement st = conn.createStatement();
//                st.executeUpdate(sql);
//                st.close();
//                conn.close();
//
//                //返回界面
//                RequestDispatcher dispatcher = req.getRequestDispatcher("stu?action=list");
//                dispatcher.forward(req, resp);
//
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }

        }else if ("modify".equals(action)) {
            String stuNo = req.getParameter("stuNo");
            Student student = studentService.getStudentByStuNo(stuNo);
            req.setAttribute("student", student);
            RequestDispatcher dispatcher = req.getRequestDispatcher("modifyStudent.jsp");
            dispatcher.forward(req, resp);
//            Student student = list.stream().filter(u -> u.getStuNo().equals(stuNo)).findFirst().orElse(null);
//
//            try {
//                //1.加载驱动
//                Class.forName("com.mysql.cj.jdbc.Driver");
//
//                //2.创建数据库的链接  jdbc:mysql://hostname:port/databasename;
//                String url = "jdbc:mysql://localhost:3306/my?serverTimezone=GMT%2B8";
//                String user = "root";
//                String pwd = "1234";
//                Connection conn = DriverManager.getConnection(url, user, pwd);
//
//                //3.构造命令并执行命令
//                String sql = "select * from students where stuNo = ?";
//                PreparedStatement ps = conn.prepareStatement(sql);
//                ps.setString(1,stuNo);
//                ResultSet rs = ps.executeQuery();
//                Student student = new Student();
//                while (rs.next()) {
//                    student.setStuNo(rs.getString(1));
//                    student.setName(rs.getString(2));
//                    student.setStuGender(rs.getString(3));
//                    student.setStuGrade(rs.getString(4));
//                    student.setStuDepart(rs.getString(5));
//                    student.setStuMajor(rs.getString(6));
//                    student.setStuClass(rs.getString(7));
//                    break;
//                }
//                req.setAttribute("student", student);
//                RequestDispatcher dispatcher = req.getRequestDispatcher("modifyStudent.jsp");
//                dispatcher.forward(req, resp);
//
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }



        }else if (action.equals("modify_sava")){
            String name = req.getParameter("name");
            String stuNo = req.getParameter("stuNo");
            String gender = req.getParameter("gender");
            String grade = req.getParameter("grade");
            String depart = req.getParameter("depart");
            String major = req.getParameter("major");
            String stuClass = req.getParameter("class");

            Student student = new Student();
            student.setName(name);
            student.setStuNo(stuNo);
            student.setStuGender(gender);
            student.setStuGrade(grade);
            student.setStuDepart(depart);
            student.setStuMajor(major);
            student.setStuClass(stuClass);

            studentService.updateStudent(student);
            RequestDispatcher dispatcher = req.getRequestDispatcher("stu?action=list");
            dispatcher.forward(req, resp);

//            try {
//                //1.加载驱动
//                Class.forName("com.mysql.cj.jdbc.Driver");
//
//                //2.创建数据库的链接  jdbc:mysql://hostname:port/databasename;
//                String url = "jdbc:mysql://localhost:3306/my?serverTimezone=GMT%2B8";
//                String user = "root";
//                String pwd = "1234";
//                Connection conn = DriverManager.getConnection(url, user, pwd);
//
//                //3.构造命令并执行命令
//                String sql = "update students set name = ?, stuGender = ?, stuGrade = ?, stuDepart = ?, stuMajor = ?, stuClass = ? WHERE stuNo = ?";
//                PreparedStatement ps = conn.prepareStatement(sql);
//                ps.setString(1,name);
//                ps.setString(2,gender);
//                ps.setString(3,grade);
//                ps.setString(4,depart);
//                ps.setString(5,major);
//                ps.setString(6,stuClass);
//                ps.setString(7,stuNo);
//                ps.executeUpdate();
//                ps.close();
//                conn.close();
//
//                RequestDispatcher dispatcher = req.getRequestDispatcher("stu?action=list");
//                dispatcher.forward(req, resp);
//
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }

        }

    }
}
